package com.blog.blogservice.infra.mapper;

import com.baomidou.mybatisplus.core.batch.MybatisBatch;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author guanglin.ma
 * @date 2024-04-11 17:02
 */
public interface MybatisBathchMapper<T> {


    default List<BatchResult> insertBathch(SqlSessionFactory sqlSessionFactory, List<T> dataList, Class mapperClass) {
        MybatisBatch<T> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, dataList);
        MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mapperClass);
        return mybatisBatch.execute(method.insert());
    }

    default List<BatchResult> updateByIdBathch(SqlSessionFactory sqlSessionFactory, List<T> dataList, Class mapperClass) {
        MybatisBatch<T> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, dataList);
        MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mapperClass);
        return mybatisBatch.execute(method.updateById());
    }

    default List<BatchResult> deleteByIdBathch(SqlSessionFactory sqlSessionFactory, List<T> dataList, Class mapperClass) {
        MybatisBatch<T> mybatisBatch = new MybatisBatch<>(sqlSessionFactory, dataList);
        MybatisBatch.Method<T> method = new MybatisBatch.Method<>(mapperClass);
        return mybatisBatch.execute(method.deleteById());
    }
}
